#!/bin/ksh





#=======================================================================
#
#
# ConfigureWebSMWebStart - configure WebSM for webstart
#
#
#=======================================================================

ConfigureWebSMWebStart()
{
  pwd=${PWD}
  cd /usr/websm/html

  # get hostname
  _hostname=`hostname`

  if [ "$_hostname" = "localhost" ];then
        return;
  fi

  # try to set the fully qualified name
  if [ -f /etc/HOSTNAME ]; then
    _hostname=`cat /etc/HOSTNAME`
  fi
 
  # if for some reason it wasnt in HOSTNAME, try resolv.conf
  if [ -f /etc/resolv.conf ]; then
    _domain=`grep search /etc/resolv.conf | awk '{print $2}' `

    # check if the domain is in the hostname, ie is it fully qualified
    if [ ! -z "$_domain" ]; then
      echo $_hostname | grep $_domain
      if [ $? -eq  1 ]; then
        # not fully qualified, so append the domain to the end
        _hostname=$_hostname.$_domain
      fi
    fi
  fi

  # copy the original files to the new files
  cp -f wsm.jnlp.org wsm.jnlp 2>/dev/null
  cp -f wsm-ext.jnlp.org wsm-ext.jnlp 2>/dev/null
  cp -f wsm-ext2.jnlp.org wsm-ext2.jnlp 2>/dev/null
  cp -f wsmssl-none.jnlp.org wsmssl-none.jnlp 2>/dev/null
  cp -f wsmssl-us.jnlp.org wsmssl-us.jnlp 2>/dev/null
  cp -f wsmssl-ex.jnlp.org wsmssl-ex.jnlp 2>/dev/null


  # change the hostname of the wsm.jnlp file
  for i in `find . -type f -name "wsm*.jnlp"`
  do
	/usr/bin/sed "s/HOSTNAME_HERE/$_hostname/g" $i > $i.new
	/bin/mv $i.new $i
  done

  # cd to / instead of previous directory because in hmc, previous directory
  # was /cdrom.  This caused the cd to be not removable
  cd /

  # setup the web browser to accept .jnlp files.  Assume default directories
  # for HTTPServer
  MIME_FILE=/etc/httpd/mime.types
  if [ -f $MIME_FILE ]; then
     grep "^application/x-java-jnlp-file" $MIME_FILE 2>&1 > /dev/null
     if [ $? -gt 0 ];then
        echo "application/x-java-jnlp-file   jnlp" >> $MIME_FILE
        echo "application/x-java-archive-diff   jardiff" >> $MIME_FILE
     fi

     # restart the server
     /etc/rc.d/apache restart 2>&1 > /dev/null
  fi

}


#=========================================================================
#
#
#                     This is the MAIN of the script
#
#
#=========================================================================


ConfigureWebSMWebStart




exit 0
